package com.yts.wechatdemo;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yts.chat.ui.activity.IMActivity;
import com.yts.wechatdemo.adapter.CreatChatGroupAdapter;
import com.yts.chat.enity.ContactModel;
import com.yts.wechatdemo.util.PinyinComparator;
import com.yts.wechatdemo.util.PinyinUtils;
import com.yts.wechatdemo.view.SideBar;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;


public class CreatChatGroupActivity extends AppCompatActivity   implements CreatChatGroupAdapter.OnItemClickListener {
    private RecyclerView mRecyclerView;
    private TextView mDialog,tv_title;
    private SideBar mSideBar;
    private ImageView img_back;
    private Button bt_sure;
    private CreatChatGroupAdapter mAdapter;
    private List<ContactModel> mContactsList = new ArrayList<ContactModel>();
    private List<ContactModel> mCheckList = new ArrayList<ContactModel>();
    /**
     * 根据拼音来排列RecyclerView里面的数据类
     */
    private PinyinComparator pinyinComparator;
    private final int[] RES_ID = new int[]{R.drawable.friend1, R.drawable.friend2, R.drawable.friend3, R.drawable.friend4, R.drawable.friend5, R.drawable.friend6,
            R.drawable.friend7, R.drawable.friend8, R.drawable.friend9, R.drawable.friend10, R.drawable.friend11, R.drawable.friend12};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creat_chat_group);
        pinyinComparator=new PinyinComparator();
        initView();
        initData();
    }

    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        img_back = (ImageView) findViewById(R.id.img_back);
        bt_sure = (Button) findViewById(R.id.bt_sure);
        bt_sure.setClickable(false);
        bt_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mCheckList.size()>0){
                    Intent intent =new Intent(CreatChatGroupActivity.this, IMActivity.class);
                    intent.putExtra("headImg",mCheckList.get(0).getHeadImg());
                    intent.putExtra("title",mCheckList.get(0).getName());
                    intent.putExtra("myHeadImg",R.drawable.friend7);
                    intent.putExtra("data", (Serializable) mCheckList);
                    intent.putExtra("type",1);
                    startActivity(intent);
                    finish();
                }

            }
        });
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        //item divider
       /* DividerItemDecoration divider = new DividerItemDecoration(getContext(), layoutManager.getOrientation());
        divider.setDrawable(getContext().getResources().getDrawable(R.drawable.common_line));
        mRecyclerView.addItemDecoration(divider);*/
        mDialog = (TextView) findViewById(R.id.dialog);
        mSideBar = (SideBar) findViewById(R.id.sideBar);
        mSideBar.setTextView(mDialog);
        mSideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = mAdapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    layoutManager.scrollToPositionWithOffset(position, 0);
                }
            }
        });

    }


    private void initData() {
        String[] names = getResources().getStringArray(R.array.names);
        mContactsList.clear();
        int i=0;
        for (;i<names.length;i++) {
            ContactModel contact = new ContactModel();
            contact.setName(names[i]);
            int rdm = new Random().nextInt(RES_ID.length);
            // Drawable drawable = getActivity().getResources().getDrawable(RES_ID[i % RES_ID.length]);
            contact.setHeadImg(RES_ID[rdm % RES_ID.length]);
            String pingYin = PinyinUtils.getPingYin(names[i]);
            //  contact.firstLetter = pingYin.substring(0, 1).toUpperCase();
            String sortString = pingYin.substring(0, 1).toUpperCase();

            // 正则表达式，判断首字母是否是英文字母
            if (sortString.matches("[A-Z]")) {
                contact.setFirstLetter(sortString.toUpperCase());
            } else {
                contact.setFirstLetter("#");
            }
            contact.setCheck(false);
            mContactsList.add(contact);
        }
        // 根据a-z进行排序源数据
        Collections.sort(mContactsList, pinyinComparator);
        mAdapter = new CreatChatGroupAdapter(mContactsList);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(View view, int position) {
        if(position==0){
            Toast.makeText(this,"看看就行了哈，不要乱摸",Toast.LENGTH_SHORT).show();
        }else {
            // Toast.makeText(getActivity(),"看看就行了哈，不要乱摸"+mContactsList.get(position).getName(),Toast.LENGTH_SHORT).show();
        }

        Intent intent =new Intent(this, IMActivity.class);
        intent.putExtra("headImg",mContactsList.get(position).getHeadImg());
        intent.putExtra("title",mContactsList.get(position).getName());
        intent.putExtra("myHeadImg",R.drawable.friend7);


        startActivity(intent);
    }

    @Override
    public void onItemCheck(boolean isCheck, int position) {
        Log.d("==============","5position==="+position);
        if(isCheck){
            bt_sure.setClickable(true);
            bt_sure.setTextColor(Color.WHITE);
            bt_sure.setBackgroundColor(Color.BLUE);
            mCheckList.add(mContactsList.get(position));
        }else {
            if (mCheckList.contains(mContactsList.get(position))){
                mCheckList.remove(mContactsList.get(position));

            }
          //  mContactsList.get(position).setCheck(isCheck);
           if(mAdapter.getCheckCount()==0){
               bt_sure.setClickable(false);
               bt_sure.setTextColor(Color.GRAY);
               bt_sure.setBackgroundColor(Color.LTGRAY);
           }
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


}
